package com.gy.mbaselibrary.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.gy.mbaselibrary.R;

public class CircleProgressView extends View {

    // 画背景的画笔
    private Paint mCirclePaint;
    // 画进度背景画笔
    private Paint mProgressBgPaint;
    // 画进度的画笔
    private Paint mProgressPaint;
    // 画中心图标的画笔
    private Paint mIconPaint;
    // 背景颜色
    private int mCircleColor;
    // 进度背景颜色
    private int mProgressBgColor;
    // 进度颜色
    private int mProgressColor;
    // 半径
    private float mRadius;
    // 进度半径
    private float mProgressRadius;
    // 进度条宽度
    private float mProgressWidth;
    // 圆心x坐标
    private int mXCenter;
    // 圆心y坐标
    private int mYCenter;
    // 总进度
    private int mTotalProgress;
    // 当前进度
    private int mProgress;
    // 中心图标大小
    private float mCenterIconWidth;

    public CircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 获取自定义的属性
        initAttrs(context, attrs);
        initVariable();
    }

    // 属性
    private void initAttrs(Context context, AttributeSet attrs) {

        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.CircleProgressView, 0, 0);

        mProgressWidth = typeArray.getDimension(R.styleable.CircleProgressView_progress_width, 10);
        mCircleColor = typeArray.getColor(R.styleable.CircleProgressView_circle_background_color, 0xFFFFFFFF);
        mProgressColor = typeArray.getColor(R.styleable.CircleProgressView_progress_color, 0xFFFFFFFF);
        mProgressBgColor = typeArray.getColor(R.styleable.CircleProgressView_progress_background_color, 0xFFFFFFFF);
        mTotalProgress = typeArray.getInt(R.styleable.CircleProgressView_total_progress, 100);
        mCenterIconWidth = typeArray.getDimension(R.styleable.CircleProgressView_center_icon_width, 20);
    }

    // 初始化画笔
    private void initVariable() {
        // 内圆
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setStyle(Paint.Style.FILL);

        // 外圆背景
        mProgressBgPaint = new Paint();
        mProgressBgPaint.setAntiAlias(true);
        mProgressBgPaint.setColor(mProgressBgColor);
        mProgressBgPaint.setStyle(Paint.Style.STROKE);
        mProgressBgPaint.setStrokeWidth(mProgressWidth);

        // 外圆弧（进度条）
        mProgressPaint = new Paint();
        mProgressPaint.setAntiAlias(true);
        mProgressPaint.setColor(mProgressColor);
        mProgressPaint.setStyle(Paint.Style.STROKE);
        mProgressPaint.setStrokeWidth(mProgressWidth);
        // 设置线冒样式，有圆，有方
//        mProgressPaint.setStrokeCap(Paint.Cap.ROUND);

        mIconPaint = new Paint();
        mIconPaint.setAntiAlias(true);
        mIconPaint.setColor(mProgressColor);
        mIconPaint.setStyle(Paint.Style.STROKE);
        mIconPaint.setStrokeWidth(mProgressWidth);
    }

    // 画图
    @Override
    protected void onDraw(Canvas canvas) {
        mXCenter = getWidth() / 2;
        mYCenter = getHeight() / 2;

        // 如果宽 > 高，半径 = 高 / 2 - 边粗
        if (getWidth() > getHeight()) {
            mRadius = mYCenter - mProgressWidth;
        } else {
            mRadius = mXCenter - mProgressWidth;
        }
        mProgressRadius = mRadius + mProgressWidth / 2;

        // 内圆
//        canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);

        // 外圆弧背景
        RectF oval1 = new RectF();
        oval1.left = (mXCenter - mProgressRadius);
        oval1.top = (mYCenter - mProgressRadius);
        oval1.right = mProgressRadius * 2 + (mXCenter - mProgressRadius);
        oval1.bottom = mProgressRadius * 2 + (mYCenter - mProgressRadius);
        // 圆弧所在的椭圆对象、圆弧的起始角度、圆弧的角度、是否显示半径连线
        canvas.drawArc(oval1, 0, 360, false, mProgressBgPaint);

        // 外圆弧
        if (mProgress > 0) {
            RectF oval2 = new RectF();
            oval2.left = (mXCenter - mProgressRadius);
            oval2.top = (mYCenter - mProgressRadius);
            oval2.right = mProgressRadius * 2 + (mXCenter - mProgressRadius);
            oval2.bottom = mProgressRadius * 2 + (mYCenter - mProgressRadius);
            canvas.drawArc(oval2, -90, ((float) mProgress / mTotalProgress) * 360, false, mProgressPaint);
        }

        mIconPaint.setStyle(Paint.Style.FILL);
        RectF oval3 = new RectF();
        oval3.left = mXCenter - (mCenterIconWidth / 2);
        oval3.top = mYCenter - (mCenterIconWidth / 2);
        oval3.right = mXCenter + (mCenterIconWidth / 2);
        oval3.bottom = mYCenter + (mCenterIconWidth / 2);
        canvas.drawRoundRect(oval3, 5, 5, mIconPaint);
    }

    // 设置进度
    public void setProgress(int progress) {
        mProgress = progress;
        postInvalidate();
    }

    // 获取进度
    public int getProgress() {
        return  mProgress;
    }
}
